/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package base;

/**
 *
 * @author Tue
 */
public class KNLink {
    private String name;//Link content
    private int sign; //-1 for -, 0 for unorientation, 1 for +
    //3 truong dia chi
    private String desHostAddr;
    //private String partLinkAddr;
    private String desNodeName;
    /**
     * Constructure of link in local machine
     * @param name
     * @param desNodeAddr
     */
    public KNLink(String name, String desNodeAddr){
        this.name = Utilities.removeSignOfLink(name);
        this.desNodeName = desNodeAddr;
        this.sign = Utilities.linkNameToSign(name);
    }
    public KNLink(String name, String desHostAddr, String desNodeAddr, int sign){
        this.name = name;
        this.desHostAddr = desHostAddr;
        this.desNodeName = desNodeAddr;
        this.sign = sign;
    }
    
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public String getNameWithSign(){
        String sign = "";
        if(this.sign == 1) sign = "+";
        else if(this.sign == -1) sign = "-";
        return sign + this.name;
    }
    public String getDesHostAddr(){
        return desHostAddr;
    }
    public String getDesNodeName(){
        return this.desNodeName;
    }
    
    public int getSign(){
        return this.sign;
    }
    public void setSign(int sign){
        this.sign = sign;
    }
    public boolean isLinkedWithOtherComputer(){
        if(desHostAddr == null)
            return false;
        else if(desHostAddr.equals(""))
            return false;
        else if(Utilities.isLocalHost(desHostAddr))
            return false;
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final KNLink other = (KNLink) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.sign != other.sign) {
            return false;
        }
        if ((this.desNodeName == null) ? (other.desNodeName != null) : !this.desNodeName.equals(other.desNodeName)) {
            return false;
        }
        return true;
    }
    
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 97 * hash + this.sign;
        hash = 97 * hash + (this.desNodeName != null ? this.desNodeName.hashCode() : 0);
        return hash;
    }

    public String toString(){
        String sign = "";
        if(this.sign == -1)
            sign = "-";
        else if(this.sign == 1) sign = "+";
        return sign + this.name + " "+ this.desNodeName;// + " "+ this.desHostAddr;
    }

}
